//
//  AuthenGeTiSettleVM.swift
//  kxf
//
//  Created by Mac on 2024/3/8.
//

import Foundation
import UIKit
import MCToast

class AuthenGeTiSettleVM: UIView {
    
    var selfHeight = kFitWidth(10)
    
    var nextBlock:(()->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: frame.size.height))
        self.selfHeight = frame.size.height
        self.backgroundColor = .COLOR_LIGHT_GREY
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var scrollView : UIScrollView = {
        let scro = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDHT, height: self.selfHeight))
        scro.backgroundColor = .COLOR_LIGHT_GREY
        if #available(iOS 11.0, *) {
            scro.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        scro.bounces = true
        
        return scro
    }()
    lazy var photoVm : AuthenGeTiSettlePhotoVM = {
        let vm = AuthenGeTiSettlePhotoVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.bankOcrFrontBlock = {(dict)in
            self.udpateBankCardBackMsg(bankMsgDict:dict)
        }
        return vm
    }()
    lazy var msgVm : AuthenGeTiSettleMsgVM = {
        let vm = AuthenGeTiSettleMsgVM.init(frame: CGRect.init(x: 0, y: self.photoVm.frame.maxY+kFitWidth(10), width: 0, height: 0))
        
        return vm
    }()
    lazy var nextBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(31), y: self.msgVm.frame.maxY+kFitWidth(40), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("下一步", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
        
        return btn
    }()
}

extension AuthenGeTiSettleVM{
    @objc func nextAction() {
        if self.nextBlock != nil{
            self.nextBlock!()
        }
    }
    func getValue() {
        AuthenCompanyMsgModel.shareInstance.settleBankNo = msgVm.bankNoVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate = msgVm.bankPrivateNameVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.settleBankType = "2"
        AuthenCompanyMsgModel.shareInstance.settleBankTypeName = "对私账户"
        AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate = msgVm.bankPrivateNameVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.settleBankPhone = msgVm.bankPhoneVm.textField.text ?? ""
    }
    func checkValue() -> Bool {
        getValue()
        
        if AuthenCompanyMsgModel.shareInstance.settleBankType == "2" && AuthenCompanyMsgModel.shareInstance.photoImgUrlBankCardFront.count < 3 {
            MCToast.mc_text("请上传银行卡卡号照")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.settleBankNo.count < 8 {
            MCToast.mc_text("请输入银行卡号")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.settleBankType.count < 1 {
            MCToast.mc_text("请选择结算账户类型")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate.count < 2 {
            MCToast.mc_text("请输入对私银行名称")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate.count < 2 {
            MCToast.mc_text("请选择对私开户支行")
            return false
        }
        if !WHBaseViewVC().judgePhoneNumber(phoneNum: AuthenCompanyMsgModel.shareInstance.settleBankPhone){
            MCToast.mc_text("请输入正确的预留手机号")
            return false
        }
        return true
    }
}
extension AuthenGeTiSettleVM{
    func initUI(){
        addSubview(scrollView)
        scrollView.addSubview(photoVm)
        scrollView.addSubview(msgVm)
        scrollView.addSubview(nextBtn)
        
        scrollView.contentSize = CGSize.init(width: 0, height: nextBtn.frame.maxY+kFitWidth(20))
    }
    
    func udpateBankCardBackMsg(bankMsgDict:NSDictionary) {
        msgVm.bankPrivateNameVm.textField.text = bankMsgDict["bank_name"]as? String ?? ""
        let bankNo = bankMsgDict["bank_card_number"]as? String ?? ""
        msgVm.bankNoVm.textField.text = bankNo.replacingOccurrences(of: " ", with: "")
//        msgVm.bankNoVm.textField.text = bankMsgDict["bank_card_number"]as? String ?? ""
    }
    func updateUIForRefuse() {
        photoVm.updateUIForRefuse()
        msgVm.updateUIForRefuse()
    }
    func setReadMode() {
        nextBtn.isHidden = true
        photoVm.isUserInteractionEnabled = false
        msgVm.isUserInteractionEnabled = false
    }
}

